package hibernateDAOS;

import hibernate.HibernateUtil;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.*;

public class CompraDAO {
	
	private static CompraDAO instancia = null;
	private static SessionFactory sf = null;
	
	public static CompraDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new CompraDAO();
		} 
		return instancia;
	}
	
	
	public  void guardarCompra(Compra c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void eliminarCompra(Compra c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarCompra(Compra c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Compra> obtenerCompras(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Compra");
		List<Compra> compras = q.list();
		session.close();
		return compras;
	}


	@SuppressWarnings("unchecked")
	public List<Compra> obtenerComprasXFecha(Integer fechaCompra)
	{
		Session session = sf.openSession();
		Query q = session.createQuery("from Compra c where c.fechaCompra = :fechaCompra").setParameter("fechaCompra", fechaCompra );
		List<Compra> compras = q.list();
		session.close();
		return compras;
	}

}
